﻿using MediatR;
using SystemWFH.API.Application.Command.ProductionPlanningCommand;
using SystemWFH.API.DTO;
using SystemWFH.Domain;
using SystemWFH.Infrstructer.Impl;
using SystemWFH.Infrstructer.Interface;

namespace SystemWFH.API.Application.CommandHandlers.ProductionPlanningCommandHandler
{
    public class ProductionPlanningGetAllCommandHandler : IRequestHandler<ProductionPlanningGetAllCommand, PageDTO<ProductionPlanning>>
    {
        private readonly IProductionPlanningRepository productionPlanning;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productionPlanning">上下文对象</param>
        public ProductionPlanningGetAllCommandHandler(IProductionPlanningRepository productionPlanning)
        {
            this.productionPlanning = productionPlanning;
        }
        public async Task<PageDTO<ProductionPlanning>> Handle(ProductionPlanningGetAllCommand request, CancellationToken cancellationToken)
        {
            PageDTO<ProductionPlanning> page = new PageDTO<ProductionPlanning>();
            var list = await productionPlanning.GetAllAsync();
            list = list.Where(x => x.IsDelete == false).ToList();
            if (!string.IsNullOrEmpty(request.theProductionPlanNumber))
            {
                list = list.Where(x => x.theProductionPlanNumber.Contains(request.theProductionPlanNumber)).ToList();
            }
            if (request.productionStatus > 0)
            {
                list = list.Where(x => x.productionStatus == request.productionStatus).ToList();
            }
            if (request.DeliveryDate!=null)
            {
                list = list.Where(x => x.DeliveryDate == request.DeliveryDate).ToList();
            }
            page.listCount = list.Count;
            page.pageCount = (int)Math.Ceiling(page.listCount * 1.0 / request.pageSize);
            page.pageList = list.OrderByDescending(x => x.productionId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

            return page;
        }
    }
}
